<%doc>
USAGE DOCUMENTATION :: PLEASE LEAVE THIS AT THE TOP OF THIS FILE

For Mako templating syntax documentation please visit: http://docs.makotemplates.org/en/latest/

Filename:           info.html
Version:            0.1
Variable names:     data [list]

data :: Usable parameters (if not applicable for media type, blank value will be returned)

== Global keys ==
rating_key              Returns the unique identifier for the media item.
media_type              Returns the type of media. Either 'movie', 'show', 'season', 'episode', 'artist', 'album', or 'track'.
sub_media_type          Returns the subtype of media. Either 'movie', 'show', 'season', 'episode', 'artist', 'album', or 'track'.
art                     Returns the location of the item's artwork
title                   Returns the name of the movie, show, episode, artist, album, or track.
edition_title            Returns the edition title of a movie.
duration                Returns the standard runtime of the media.
content_rating          Returns the age rating for the media.
summary                 Returns a brief description of the media plot.
grandparent_title       Returns the name of the show, or artist.
parent_media_index      Returns the index number of the season.
media_index             Returns the index number of the episode, or track.
parent_thumb            Returns the location of the item's thumbnail. Use with pms_image_proxy.
writers                 Returns an array of writers.
thumb                   Returns the location of the item's thumbnail. Use with pms_image_proxy.
parent_title            Returns the name of the show, or artist.
rating                  Returns the 5 star rating value for the movie. Between 1 and 5.
year                    Returns the release year of the movie, or show.
genres                  Returns an array of genres.
actors                  Returns an array of actors.
directors               Returns an array of directors.
studio                  Returns the name of the studio.
originally_available_at Returns the air date of the item.

DOCUMENTATION :: END
</%doc>

<%!
    from collections import defaultdict
    import re

    from plexpy import notifiers
    from plexpy.common import MEDIA_TYPE_HEADERS, MEDIA_FLAGS_AUDIO, MEDIA_FLAGS_VIDEO
    from plexpy.helpers import page, get_percent, cast_to_int, short_season

    # Get audio codec file
    def af(codec):
        for pattern, file_type in MEDIA_FLAGS_AUDIO.items():
            if re.match(pattern, codec):
                return file_type
        return codec

    # Get video codec file
    def vf(codec):
        for pattern, file_type in MEDIA_FLAGS_VIDEO.items():
            if re.match(pattern, codec):
                return file_type
        return codec

    # Get video resolution file
    def vr(resolution):
        if resolution in ('1080i', '576i', '480i'):
            return resolution
        else:
            return resolution.lower().rstrip('ip')

    def br(text):
        return text.replace('\n', '<br /><br />')
%>

<%inherit file="base.html"/>

<%def name="headIncludes()">
<link rel="stylesheet" href="${http_root}css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="${http_root}css/dataTables.colVis.css">
<link rel="stylesheet" href="${http_root}css/tautulli-dataTables.css">
</%def>

<%def name="body()">
% if metadata:
<%
    data = defaultdict(lambda: None, **metadata)
    media_info = defaultdict(lambda: None, **(data['media_info'][0] if data['media_info'] else {}))
    season = ''
    if data['media_type'] == 'episode':
        season = short_season(data['parent_title'])
    elif data['media_type'] == 'season':
        season = short_season(data['title'])
%>
<div class="container-fluid">
    <div class="row">
        % if data['media_type'] not in ('photo_album', 'photo', 'playlist'):
        <% fallback = 'art-live-full' if data['live'] else None %>
        <div class="art-face" style="background-image:url(${page('pms_image_proxy', data['art'], data['rating_key'], 1920, 1080, fallback=fallback)})"></div>
        % endif
        % if _session['user_group'] == 'admin':
        <span class="overlay-refresh-image info-art" title="Refresh background image"><i class="fa fa-refresh refresh_pms_image"></i></span>
        % endif
        <div class="summary-container">
            <div class="summary-navbar">
                <div class="col-md-12">
                    <div class="summary-navbar-list">
                        <ul class="list-unstyled breadcrumb">
                        % if data['live']:
                        <li><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            % if data['media_type'] == 'movie':
                            <li class="active metadata-xml">${data['title']}</li>
                            % elif data['media_type'] == 'episode':
                            <li class="hidden-xs hidden-sm">${data['grandparent_title']}</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            % if data['media_index']:
                            <li> ${data['parent_title']}</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="active metadata-xml">Episode ${data['media_index']} - ${data['title']}</li>
                            % else:
                            <li class="active metadata-xml">${data['title']}</li>
                            % endif
                            % endif
                        % elif data['media_type'] in ('movie', 'collection'):
                        <li><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li class="active metadata-xml">${data['title']}</li>
                        % elif data['media_type'] == 'show':
                        <li><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li class="active metadata-xml">${data['title']}</li>
                        % elif data['media_type'] == 'season':
                        <li class="hidden-xs hidden-sm"><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li><a href="${page('info', data['parent_rating_key'])}">${data['parent_title']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li class="active metadata-xml">${data['title']}</li>
                        % elif data['media_type'] == 'episode':
                        <li class="hidden-xs hidden-sm"><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li class="hidden-xs hidden-sm"><a href="${page('info', data['grandparent_rating_key'])}">${data['grandparent_title']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li><a href="${page('info', data['parent_rating_key'])}">${data['parent_title']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li class="active metadata-xml">Episode ${data['media_index']} - ${data['title']}</li>
                        % elif data['media_type'] == 'artist':
                        <li><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li class="active metadata-xml">${data['title']}</li>
                        % elif data['media_type'] == 'album':
                        <li class="hidden-xs hidden-sm"><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li><a href="${page('info', data['parent_rating_key'])}">${data['parent_title']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li class="active metadata-xml">${data['title']}</li>
                        % elif data['media_type'] == 'track':
                        <li class="hidden-xs hidden-sm"><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li class="hidden-xs hidden-sm"><a href="${page('info', data['grandparent_rating_key'])}">${data['grandparent_title']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li><a href="${page('info', data['parent_rating_key'])}">${data['parent_title']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li class="active metadata-xml">Track ${data['media_index']} - ${data['title']}</li>
                        % elif data['media_type'] == 'photo_album':
                        <li><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            % if data['parent_title']:
                            <li><a href="${page('info', data['parent_rating_key'])}">${data['parent_title']}</a></li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            % endif
                        <li class="active metadata-xml">${data['title']}</li>
                        % elif data['media_type'] in ('photo', 'clip'):
                        <li class="hidden-xs hidden-sm"><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li><a href="${page('info', data['parent_rating_key'])}">${data['parent_title']}</a></li>
                        <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                        <li class="active metadata-xml">${data['title']}</li>
                        % elif data['media_type'] == 'playlist':
                            % if user_info.get('user_id'):
                            <li><a href="${page('user', user_info.get('user_id'))}">${user_info.get('friendly_name')}</a></li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            % elif data['section_id']:
                            <li><a href="${page('library', data['section_id'])}">${data['library_name']}</a></li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            % endif
                        <li class="active metadata-xml">${data['title']}</li>
                        % endif
                        </ul>
                    </div>
                </div>
            </div>
            <div class="summary-content-title-wrapper">
                <div class="col-md-9">
                    <div class="summary-content-poster hidden-xs hidden-sm">
                        <% legacy = '&legacy=1' if data['media_type'] in ('photo_album', 'photo', 'clip') else '' %>
                        % if data['media_type'] in ('track', 'photo'):
                        <a href="${config['pms_web_url']}#!/server/${config['pms_identifier']}/details?key=%2Flibrary%2Fmetadata%2F${data['parent_rating_key']}${legacy}" target="_blank" rel="noreferrer" title="View on Plex Web">
                        % elif data['media_type'] == 'playlist':
                        <a href="${config['pms_web_url']}#!/server/${config['pms_identifier']}/playlist?key=%2Fplaylists%2F${data['rating_key']}" target="_blank" rel="noreferrer" title="View on Plex Web">
                        % elif not data['live']:
                        <a href="${config['pms_web_url']}#!/server/${config['pms_identifier']}/details?key=%2Flibrary%2Fmetadata%2F${data['rating_key']}${legacy}" target="_blank" rel="noreferrer" title="View on Plex Web">
                        % endif
                        % if data['live']:
                            <div class="summary-poster-face" style="background-image: url(${page('pms_image_proxy', data['grandparent_thumb'] or data['thumb'], data['rating_key'], 300, 450, fallback='poster-live')});">
                                <div class="summary-poster-face-overlay">
                                    <span></span>
                                </div>
                            </div>
                        % else:
                            % if data['media_type'] == 'episode':
                            <div class="summary-poster-face-episode" style="background-image: url(${page('pms_image_proxy', data['thumb'], data['rating_key'], 500, 280, fallback='art')});">
                                <div class="summary-poster-face-overlay">
                                    <span></span>
                                </div>
                            </div>
                            % if _session['user_group'] == 'admin':
                            <span class="overlay-refresh-image" title="Refresh image"><i class="fa fa-refresh refresh_pms_image"></i></span>
                            % endif
                            % elif data['media_type'] in ('artist', 'album', 'track', 'playlist', 'photo_album', 'photo', 'clip') or data['sub_media_type'] in ('artist', 'album', 'track'):
                            <div class="summary-poster-face-track" style="background-image: url(${page('pms_image_proxy', data['thumb'], data['rating_key'], 300, 300, fallback='cover')});">
                                <div class="summary-poster-face-overlay">
                                    <span></span>
                                </div>
                                    % if data['media_type'] == 'playlist' and data['smart']:
                                    <span class="smart-playlist-image" title="Smart Playlist"><i class="fa fa-cog"></i></span>
                                    % endif
                            </div>
                            % if _session['user_group'] == 'admin':
                            <span class="overlay-refresh-image" title="Refresh image"><i class="fa fa-refresh refresh_pms_image"></i></span>
                            % endif
                            % else:
                            <div class="summary-poster-face" style="background-image: url(${page('pms_image_proxy', data['thumb'], data['rating_key'], 300, 450, fallback='poster')});">
                                <div class="summary-poster-face-overlay">
                                    <span></span>
                                </div>
                                    % if data['media_type'] == 'collection' and data['smart']:
                                    <span class="smart-collection-image" title="Smart Collection"><i class="fa fa-cog"></i></span>
                                    % endif
                            </div>
                            % if _session['user_group'] == 'admin':
                            <span class="overlay-refresh-image" title="Refresh image"><i class="fa fa-refresh refresh_pms_image"></i></span>
                            % endif
                            % endif
                        % endif
                        % if not data['live']:
                        </a>
                        % endif
                    </div>
                    <div class="summary-content-title">
                        % if data['live']:
                            % if data['media_type'] == 'movie':
                            <h1>&nbsp;</h1><h1>${data['title']}</h1>
                            % elif data['media_type'] == 'episode':
                            <h1>${data['grandparent_title']}</h1>
                            <h2>${data['title']}</h2>
                            % if data['media_index']:
                            <h3 class="hidden-xs">${season} &middot; E${data['media_index']}</h3>
                            % endif
                            % endif
                        % elif data['media_type'] in ('movie', 'show', 'artist', 'collection', 'playlist', 'photo_album'):
                        <h1>&nbsp;</h1><h1>${data['title']}</h1>
                        % elif data['media_type'] == 'season':
                        <h1>&nbsp;</h1><h1><a href="${page('info', data['parent_rating_key'])}">${data['parent_title']}</a></h1>
                        <h3 class="hidden-xs">${season}</h3>
                        % elif data['media_type'] == 'episode':
                        <h1><a href="${page('info', data['grandparent_rating_key'])}">${data['grandparent_title']}</a></h1>
                        <h2>${data['title']}</h2>
                        <h3 class="hidden-xs">${season} &middot; E${data['media_index']}</h3>
                        % elif data['media_type'] == 'album':
                        <h1><a href="${page('info', data['parent_rating_key'])}">${data['parent_title']}</a></h1>
                        <h2>${data['title']}</h2>
                        % elif data['media_type'] == 'track':
                        <h1><a href="${page('info', data['grandparent_rating_key'])}">${data['grandparent_title']}</a></h1>
                        <h2><a href="${page('info', data['parent_rating_key'])}">${data['parent_title']}</a> - ${data['title']}</h2>
                        <h3 class="hidden-xs">T${data['media_index']}</h3>
                        % elif data['media_type'] in ('photo', 'clip'):
                        <h1><a href="${page('info', data['parent_rating_key'])}">${data['parent_title']}</a></h1>
                        <h2>${data['title']}</h2>
                        % endif
                    </div>
                </div>
            </div>
            <div class="summary-content-wrapper">
                <div class="col-md-9">
                    <%
                        padding_height = ''
                        if data['media_type'] == 'movie' or data['live']:
                            padding_height = 'height: 305px;'
                        elif data['media_type'] in ('artist', 'album', 'playlist', 'photo_album', 'photo') or data['sub_media_type'] in ('artist', 'album', 'track'):
                            padding_height = 'height: 150px;'
                        elif data['media_type'] in ('track', 'clip'):
                            padding_height = 'height: 180px;'
                        elif data['media_type'] == 'episode':
                            padding_height = 'height: 70px;'
                        elif data['media_type'] in ('show', 'season', 'collection'):
                            padding_height = 'height: 270px;'
                    %>
                    <div class="summary-content-padding hidden-xs hidden-sm" style="${padding_height}">
                        % if data['media_type'] in ('movie', 'episode', 'track', 'clip'):
                        <div class="summary-content-media-info-wrapper">
                            % if data['media_type'] != 'track' and media_info['video_codec']:
                            <img class="summary-content-media-flag" title="${media_info['video_codec']}" src="${http_root}images/media_flags/video_codec/${media_info['video_codec'] | vf}.png" />
                            % endif
                            % if data['media_type'] != 'track' and media_info['video_resolution']:
                            <img class="summary-content-media-flag" title="${media_info['video_resolution']}" src="${http_root}images/media_flags/video_resolution/${media_info['video_full_resolution'] | vr}.png" />
                            % endif
                            % if media_info['audio_codec']:
                            <img class="summary-content-media-flag" title="${media_info['audio_codec']}" src="${http_root}images/media_flags/audio_codec/${media_info['audio_codec'] | af}.png" />
                            % endif
                            % if media_info['audio_channels']:
                            <img class="summary-content-media-flag" title="${media_info['audio_channels']}" src="${http_root}images/media_flags/audio_channels/${media_info['audio_channels']}.png" />
                            % endif
                        </div>
                        % endif
                    </div>
                    <div class="summary-content">
                        <div class="summary-content-details-wrapper">
                            <% rating = data['rating'] or data['audience_rating'] %>
                            % if rating:
                            % if data['audience_rating_image']:
                            % if data['audience_rating_image'].startswith('imdb://'):
                            <div class="critic-rating hidden-xs hidden-sm" title="${rating}">
                                <span class="rating-image rating-imdb"></span><strong>${rating}</strong>
                            </div>
                            % endif
                            % if data['audience_rating_image'].startswith('themoviedb://'):
                            <div class="critic-rating hidden-xs hidden-sm" title="${rating}">
                                <span class="rating-image rating-themoviedb"></span><strong>${get_percent(rating, 10)}%</strong>
                            </div>
                            % endif
                            % if data['audience_rating_image'].startswith('thetvdb://'):
                            <div class="critic-rating hidden-xs hidden-sm" title="${rating}">
                                <span class="rating-image rating-thetvdb"></span><strong>${get_percent(rating, 10)}%</strong>
                            </div>
                            % endif
                            % if data['audience_rating_image'].startswith('rottentomatoes://'):
                            <div class="critic-rating hidden-xs hidden-sm" title="${data['audience_rating']}">
                                <span class="rating-image rating-rottentomatos-${data['audience_rating_image'].rsplit('.')[-1]}"></span><strong>${get_percent(data['audience_rating'], 10)}%</strong>
                            </div>
                            % endif
                            % if data['rating_image'].startswith('rottentomatoes://'):
                            <div class="critic-rating hidden-xs hidden-sm" title="${data['rating']}">
                                <span class="rating-image rating-rottentomatos-${data['rating_image'].rsplit('.')[-1]}"></span><strong>${get_percent(data['rating'], 10)}%</strong>
                            </div>
                            % endif
                            % else:
                            <div class="critic-rating hidden-xs hidden-sm" title="${rating}">
                                <i class="star-icon fa fa-star"></i> <strong>${get_percent(rating, 10)}%</strong>
                            </div>
                            % endif
                            % endif
                            <div class="summary-content-details-tag">
                                % if data['media_type'] in ('collection', 'playlist') and data['children_count']:
                                <%
                                    if data['media_type'] == 'collection':
                                        suffix = MEDIA_TYPE_HEADERS[data['sub_media_type']]
                                    elif data['media_type'] == 'playlist':
                                        suffix = MEDIA_TYPE_HEADERS[data['playlist_type']]
                                    if data['children_count'] == 1:
                                        suffix = suffix[:-1]
                                %>
                                Items <strong> ${data['children_count']} ${suffix} </strong>
                                % endif
                            </div>
                            <div class="summary-content-details-tag">
                                % if data['directors']:
                                Directed by <strong> ${data['directors'][0]}</strong>
                                % endif
                            </div>
                            <div class="summary-content-details-tag">
                                % if data['studio']:
                                Studio <strong> ${data['studio']}</strong>
                                % endif
                            </div>
                            <div class="summary-content-details-tag">
                                % if data['media_type'] == 'track' and data['original_title']:
                                Track Artists <strong> ${data['original_title']}</strong>
                                % endif
                            </div>
                            <div class="summary-content-details-tag">
                                % if data['media_type'] == 'movie':
                                Year <strong> ${data['year']}</strong>
                                % elif data['media_type'] == 'show':
                                Aired <strong> ${data['year']}</strong>
                                % elif data['media_type'] == 'episode':
                                Aired <strong> <span id="airdate">${data['originally_available_at']}</span></strong>
                                % elif data['media_type'] == 'album' or data['media_type'] == 'track':
                                Released <strong> ${data['year']}</strong>
                                % elif data['media_type'] in ('photo', 'clip'):
                                Taken <strong> <span id="airdate">${data['originally_available_at']}</span></strong>
                                % elif data['media_type'] == 'collection' and data['min_year'] and data['max_year']:
                                Year <strong> ${data['min_year']} - ${data['max_year']}</strong>
                                % elif data['year']:
                                Year <strong> ${data['year']}</strong>
                                % endif
                            </div>
                            <div class="summary-content-details-tag">
                                % if data['duration']:
                                Runtime <strong> <span id="runtime">${data['duration']}</span></strong>
                                % endif
                            </div>
                            % if data['edition_title']:
                            <div class="summary-content-details-tag">
                                Edition <strong> ${data['edition_title']} </strong>
                            </div>
                            % endif
                            <div class="summary-content-details-tag">
                                % if data['content_rating']:
                                Rated <strong> ${data['content_rating']} </strong>
                                % endif
                            </div>
                            <div class="summary-content-details-tag" id="channel-icon">
                                % if media_info['channel_vcn']:
                                Channel <strong> <span class="thumb-tooltip" data-toggle="popover" data-img="${media_info['channel_thumb']}" data-height="40" data-width="40">${media_info['channel_title'] or (media_info['channel_vcn'] + ' ' + media_info['channel_call_sign'])}</span> </strong>
                                % endif
                            </div>
                        </div>
                        % if data['tagline']:
                        <div class="summary-content-text summary-content-tagline">
                            <p>${data['tagline']}</p>
                        </div>
                        % endif
                        <div class="summary-content-text summary-content-summary">
                            <p>${data['summary'] | br, n}</p>
                        </div>
                        <div class="summary-content-text summary-toggle">
                            <a href="#" class="show-more">Read more &nbsp;<i class="fa fa-chevron-down"></i></a>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="summary-content-people-wrapper hidden-xs hidden-sm">
                        % if data['writers']:
                        <div class="summary-content-writers">
                            <strong>Written by</strong>
                            <ul>
                                % for writer in data['writers']:
                                % if loop.index < 5:
                                <li>
                                    ${writer}
                                </li>
                                % endif
                                % endfor
                            </ul>
                        </div>
                        % endif
                        % if data['actors']:
                        <div class="summary-content-actors">
                            <strong>Starring</strong>
                            <ul>
                                % for actor in data['actors']:
                                % if loop.index < 5:
                                <li>
                                    ${actor}
                                </li>
                                % endif
                                % endfor
                            </ul>
                        </div>
                        % endif
                    </div>
                    <div class="summary-content-people-wrapper hidden-xs hidden-sm">
                        % if data['genres']:
                        <div class="summary-content-genres">
                            <strong>Genres</strong>
                            <ul>
                                % for genre in data['genres']:
                                % if loop.index < 5:
                                <li>
                                    ${genre}
                                </li>
                                % endif
                                % endfor
                            </ul>
                        </div>
                        % endif
                    </div>
                </div>
                % if data['media_type'] == 'show':
                <div class="col-md-12">
                    <div class="table-card-header">
                        <div class="header-bar">
                            <span>Season List for <strong>${data['title']}</strong></span>
                        </div>
                    </div>
                    <div class="table-card-back">
                        <div id="children-list" class="children-list"><i class="fa fa-refresh fa-spin"></i>&nbsp; Loading season list...</div>
                    </div>
                </div>
                % elif data['media_type'] == 'season':
                <div class="col-md-12">
                    <div class="table-card-header">
                        <div class="header-bar">
                            <span>Episode List for <strong>${data['title']}</strong></span>
                        </div>
                    </div>
                    <div class="table-card-back">
                        <div id="children-list" class="children-list"><i class="fa fa-refresh fa-spin"></i>&nbsp; Loading episode list...</div>
                    </div>
                </div>
                % elif data['media_type'] == 'artist':
                <div class="col-md-12">
                    <div class="table-card-header">
                        <div class="header-bar">
                            <span>Album List for <strong>${data['title']}</strong></span>
                        </div>
                    </div>
                    <div class="table-card-back">
                        <div id="children-list" class="children-list"><i class="fa fa-refresh fa-spin"></i>&nbsp; Loading album list...</div>
                    </div>
                </div>
                % elif data['media_type'] == 'album':
                <div class="col-md-12">
                    <div class="table-card-header">
                        <div class="header-bar">
                            <span>Track List for <strong>${data['title']}</strong></span>
                        </div>
                    </div>
                    <div class="table-card-back">
                        <div id="children-list" class="children-list"><i class="fa fa-refresh fa-spin"></i>&nbsp; Loading track list...</div>
                    </div>
                </div>
                % elif data['media_type'] == 'photo_album':
                <div class="col-md-12">
                    <div class="table-card-header">
                        <div class="header-bar">
                            <span>Photo List for <strong>${data['title']}</strong></span>
                        </div>
                    </div>
                    <div class="table-card-back">
                        <div id="children-list" class="children-list"><i class="fa fa-refresh fa-spin"></i>&nbsp; Loading photo list...</div>
                    </div>
                </div>
                % elif data['media_type'] == 'collection':
                <div class="col-md-12">
                    <div class="table-card-header">
                        <div class="header-bar">
                            <span>${MEDIA_TYPE_HEADERS[data['sub_media_type']]} in <strong>${data['title']}</strong> collection</span>
                        </div>
                    </div>
                    <div class="table-card-back">
                        <div id="children-list" class="children-list"><i class="fa fa-refresh fa-spin"></i>&nbsp; Loading collection items...</div>
                    </div>
                </div>
                <div id="collection-related-list-container" style="display: none;">
                </div>
                % elif data['media_type'] == 'playlist':
                <div class="col-md-12">
                    <div class="table-card-header">
                        <div class="header-bar">
                            <span>${MEDIA_TYPE_HEADERS[data['playlist_type']]} List for <strong>${data['title']}</strong></span>
                        </div>
                    </div>
                    <div class="table-card-back">
                        <div id="children-list" class="children-list"><i class="fa fa-refresh fa-spin"></i>&nbsp; Loading playlist items...</div>
                    </div>
                </div>
                % endif
                % if data['media_type'] in ('movie', 'show', 'season', 'episode', 'artist', 'album', 'track', 'collection', 'playlist'):
                <div class="col-md-12">
                    <div class="table-card-header">
                        <div class="header-bar">
                            <span><i class="fa fa-line-chart"></i> Global Stats</span>
                        </div>
                    </div>
                    <div class="table-card-back">
                        <div id="watch-time-stats" class="user-overview-stats-wrapper">
                            <div class='muted'><i class="fa fa-refresh fa-spin"></i> Loading data...</div>
                            <br>
                        </div>
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="table-card-header">
                        <div class="header-bar">
                            <span><i class="fa fa-group"></i> User Stats</span>
                        </div>
                    </div>
                    <div class="table-card-back">
                        <div id="user-stats" class="user-player">
                            <div class='muted'><i class="fa fa-refresh fa-spin"></i> Loading data...</div>
                            <br>
                        </div>
                    </div>
                </div>
                % endif
                <%
                    history_type = data['media_type'] in ('movie', 'show', 'season', 'episode', 'artist', 'album', 'track', 'collection', 'playlist')
                    history_active = 'active' if history_type else ''
                    export_active = 'active' if not history_type else ''
                %>
                % if history_type and _session['user_group'] == 'admin':
                <div class="col-md-12">
                    <div class="table-card-header">
                        <ul class="nav nav-list nav-pills" role="tablist">
                            <li class="${history_active}"><a id="nav-tabs-history" href="#tabs-history" role="tab" data-toggle="tab">History</a></li>
                            <li class="${export_active}"><a id="nav-tabs-export" href="#tabs-export" role="tab" data-toggle="tab">Export</a></li>
                        </ul>
                        <div class="button-bar">
                            % if source == 'history':
                            <div class="btn-group">
                                <a href="update_metadata?rating_key=${data['rating_key']}&update=True" class="btn btn-danger btn-edit" id="fix-metadata">
                                    <i class="fa fa-wrench"></i> Fix Match
                                </a>
                            </div>
                            % endif
                            % if data.get('tvmaze_id') or data.get('themoviedb_id') or data.get('musicbrainz_id'):
                            <div class="btn-group">
                                <button class="btn btn-danger btn-edit" data-toggle="modal" aria-pressed="false" autocomplete="off" id="delete-lookup-info"
                                        data-id="${data['grandparent_rating_key'] if data['media_type'] == 'episode' else data['parent_rating_key'] if data['media_type'] == 'season' else data['rating_key']}"
                                        data-title="${data['grandparent_title'] if data['media_type'] == 'episode' else data['parent_title'] if data['media_type'] == 'season' else data['title']}">
                                    <i class="fa fa-search"></i> Delete Lookup Info
                                </button>
                            </div>
                            % endif
                            % if data.get('poster_url'):
                            <div class="btn-group" id="hosted-poster">
                                % if data['media_type'] == 'artist' or data['media_type'] == 'album' or data['media_type'] == 'track':
                                <span class="hosted-poster-tooltip" data-toggle="popover" data-img="${data['poster_url']}" data-height="80" data-width="80" style="display: inline-flex;">
                                % else:
                                <span class="hosted-poster-tooltip" data-toggle="popover" data-img="${data['poster_url']}" data-height="120" data-width="80" style="display: inline-flex;">
                                % endif
                                    <button class="btn btn-danger btn-edit" data-toggle="modal" aria-pressed="false" autocomplete="off" id="delete-hosted-poster"
                                            data-id="${data['parent_rating_key'] if data['media_type'] in ('episode', 'track') else data['rating_key']}"
                                            data-title="${data["poster_title"]}">
                                        <i class="fa fa-picture-o"></i> Delete ${data['img_service']} Poster
                                    </button>
                                </span>
                            </div>
                            % endif
                            % if not data['live']:
                            <div class="btn-group">
                                <button class="btn btn-dark" data-toggle="modal" aria-pressed="false" autocomplete="off" id="send-recently-added-notification"
                                        data-id="${data['rating_key']}">
                                    <i class="fa fa-bell"></i> Recently Added Notification
                                </button>
                            </div>
                            % endif
                        </div>
                    </div>
                </div>
                % endif
                <div class="tab-content">
                    % if history_type:
                    <div role="tabpanel" class="tab-pane ${history_active}" id="tabs-history">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="table-card-header">
                                        <div class="header-bar">
                                            % if data['media_type'] in ('artist', 'album', 'track', 'playlist'):
                                            <span>Play History for <strong>${data['title']}</strong></span>
                                            % else:
                                            <span>Watch History for <strong>${data['title']}</strong></span>
                                            % endif
                                        </div>
                                        <div class="button-bar">
                                            % if _session['user_group'] == 'admin':
                                            <div class="btn-group">
                                                <button class="btn btn-danger btn-edit" data-toggle="button" aria-pressed="false" autocomplete="off" id="row-edit-mode">
                                                    <i class="fa fa-trash-o"></i> Delete mode
                                                </button>
                                            </div>
                                            % endif
                                            <div class="btn-group" data-toggle="buttons" id="transcode_decision-selection">
                                                <label class="btn btn-dark btn-filter">
                                                    <input type="checkbox" name="transcode_decision-filter" id="history-transcode_decision-direct_play" value="direct play" autocomplete="off"><i class="fa fa-play-circle"></i> Direct Play
                                                </label>
                                                <label class="btn btn-dark btn-filter">
                                                    <input type="checkbox" name="transcode_decision-filter" id="history-transcode_decision-direct_stream" value="copy" autocomplete="off"><i class="fa fa-stream"></i> Direct Stream
                                                </label>
                                                <label class="btn btn-dark btn-filter">
                                                    <input type="checkbox" name="transcode_decision-filter" id="history-transcode_decision-transcode" value="transcode" autocomplete="off"><i class="fa fa-server"></i> Transcode
                                                </label>
                                            </div>
                                            <div class="btn-group">
                                                <button class="btn btn-dark refresh-history-button" id="refresh-history-list"><i class="fa fa-refresh"></i> Refresh history</button>
                                            </div>
                                            <div class="btn-group colvis-button-bar" id="button-bar-history"></div>
                                        </div>
                                    </div>
                                    <div class="table-card-back">
                                        <table class="display history_table" id="history_table-RK-${data['rating_key']}" width="100%">
                                            <thead>
                                                <tr>
                                                    <th align="left" id="delete">Delete</th>
                                                    <th align="left" id="date">Date</th>
                                                    <th align="left" id="friendly_name">User</th>
                                                    <th align="left" id="ip_address">IP Address</th>
                                                    <th align="left" id="platform">Platform</th>
                                                    <th align="left" id="product">Product</th>
                                                    <th align="left" id="player">Player</th>
                                                    <th align="left" id="title">Title</th>
                                                    <th align="left" id="started">Started</th>
                                                    <th align="left" id="paused_counter">Paused</th>
                                                    <th align="left" id="stopped">Stopped</th>
                                                    <th align="left" id="play_duration">Duration</th>
                                                    <th align="left" id="percent_complete"></th>
                                                </tr>
                                            </thead>
                                            <tbody></tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    % endif
                    % if not data['live'] and _session['user_group'] == 'admin':
                    <div role="tabpanel" class="tab-pane ${export_active}" id="tabs-export">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="table-card-header">
                                        <div class="header-bar">
                                            <span>Metadata Exports for <strong>${data['title']}</strong></span>
                                        </div>
                                        <div class="button-bar">
                                            <div class="btn-group">
                                                <button class="btn btn-dark export-button" id="toggle-export-modal" data-toggle="modal" data-target="#export-modal"
                                                        data-section_id="${data['section_id']}" data-rating_key="${data['rating_key']}"
                                                        data-media_type="${data['media_type']}" data-sub_media_type="${data['sub_media_type'] or data['playlist_type'] or ''}">
                                                    <i class="fa fa-file-export"></i> Export metadata
                                                </button>
                                            </div>
                                            <div class="btn-group">
                                                <button class="btn btn-dark refresh-export-table-button" id="refresh-export-table">
                                                    <i class="fa fa-refresh"></i> Refresh exports
                                                </button>
                                            </div>
                                            <div class="btn-group colvis-button-bar" id="button-bar-export"></div>
                                        </div>
                                    </div>
                                    <div class="table-card-back">
                                        <table class="display export_table" id="export_table-RK-${data['rating_key']}" width="100%">
                                            <thead>
                                                <tr>
                                                    <th align="left" id="timestamp">Exported At</th>
                                                    <th align="left" id="media_type_title">Media Type</th>
                                                    <th align="left" id="rating_key">Rating Key</th>
                                                    <th align="left" id="filename">Filename</th>
                                                    <th align="left" id="file_format">File Format</th>
                                                    <th align="left" id="metadata_level">Metadata Level</th>
                                                    <th align="left" id="media_info_level">Media Info Level</th>
                                                    <th align="left" id="media_info_level">Custom Fields</th>
                                                    <th align="left" id="file_size">File Size</th>
                                                    <th align="left" id="complete">Download</th>
                                                    <th align="left" id="delete">Delete</th>
                                                </tr>
                                            </thead>
                                            <tbody></tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    % endif
                </div>
            </div>
        </div>
    </div>
</div>
% endif
</%def>

<%def name="modalIncludes()">
% if metadata:
<%
    data = defaultdict(None, **metadata)
    season = ''
    if data['media_type'] == 'episode':
        season = short_season(data['parent_title'])
    elif data['media_type'] == 'season':
        season = short_season(data['title'])
%>
<div class="modal fade" id="info-modal" tabindex="-1" role="dialog" aria-labelledby="info-modal">
</div>
<div class="modal fade" id="ip-info-modal" tabindex="-1" role="dialog" aria-labelledby="ip-info-modal">
</div>
<div class="modal fade" id="confirm-modal-delete" tabindex="-1" role="dialog" aria-labelledby="confirm-modal-delete">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-remove"></i></button>
                <h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
            </div>
            <div class="modal-body" style="text-align: center;">
                <p>Are you REALLY sure you want to delete <strong><span id="deleteCount"></span></strong> history item(s)?</p>
                <p>This is permanent and cannot be undone!</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-dark" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-danger btn-ok" data-dismiss="modal" id="confirm-delete">Delete</button>
            </div>
        </div>
    </div>
</div>
<div id="send-recently-added-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="send-recently-added-modal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-remove"></i></button>
                <h4 class="modal-title">Recently Added Notification</h4>
            </div>
            <div class="modal-body" style="text-align: center;">
                <p>Send a recently added notification for the following:</p>
                <p>
                    <strong>
                        % if data['media_type'] == 'movie':
                        ${data['title']}<br />${data['year']}
                        % elif data['media_type'] == 'show':
                        ${data['title']}
                        % elif data['media_type'] == 'season':
                        ${data['parent_title']}<br />${data['title']}
                        % elif data['media_type'] == 'episode':
                        ${data['grandparent_title']}<br />${data['title']}<br />${season} &middot; E${data['media_index']}
                        % elif data['media_type'] == 'artist':
                        ${data['title']}
                        % elif data['media_type'] == 'album':
                        ${data['parent_title']}<br />${data['title']}
                        % elif data['media_type'] == 'track':
                        ${data['grandparent_title']}<br />${data['title']}<br />${data['parent_title']}
                        % endif
                    </strong>
                </p>
                <br />
                <label for="send-notification-notifier">Select the Notification Agent</label>
                <div class="row">
                    <div class="col-md-8" style="float: none; margin: 0 auto;">
                        <select class="form-control" id="send-notification-notifier" name="send-notification-notifier" value="" placeholder="The server owner has ended the stream.">
                            <option value="">All Enabled Recently Added Notification Agents</option>
                            % for notifier in sorted(notifiers.get_notifiers(), key=lambda k: (k['agent_label'], k['friendly_name'], k['id'])):
                            % if notifier['friendly_name']:
                            <option value="${notifier['id']}"> ${notifier['agent_label']} (${notifier['id']} - ${notifier['friendly_name']})</option>
                            % else:
                            <option value="${notifier['id']}"> ${notifier['agent_label']} (${notifier['id']})</option>
                            % endif
                            % endfor
                        </select>
                    </div>
                    <p class="help-block">Note: All custom notification conditions will be bypassed.</p>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-dark" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-bright btn-ok" data-dismiss="modal" id="confirm-send-notification">Send</button>
            </div>
        </div>
    </div>
</div>
% endif
<div id="export-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="export-modal">
</div>
</%def>

<%def name="javascriptIncludes()">
<script src="${http_root}js/jquery.dataTables.min.js"></script>
<script src="${http_root}js/dataTables.colVis.js"></script>
<script src="${http_root}js/dataTables.bootstrap.min.js"></script>
<script src="${http_root}js/dataTables.bootstrap.pagination.js"></script>

% if metadata:
<%
    data = defaultdict(None, **metadata)
    history_user_id = '' if _session['user_group'] == 'admin' else _session['user_id']
%>
<script src="${http_root}js/tables/history_table.js${cache_param}"></script>
<script src="${http_root}js/tables/export_table.js${cache_param}"></script>
% if data['media_type'] in ('movie', 'show', 'season', 'episode', 'artist', 'album', 'track', 'collection', 'playlist'):
<script>
    function loadHistoryTable(transcode_decision) {
        // Build watch history table
        history_table_options.ajax = {
            url: 'get_history',
            type: 'POST',
            data: function (d) {
                return {
                    json_data: JSON.stringify(d),
                    transcode_decision: transcode_decision,
                    user_id: "${history_user_id}",
                    % if data['live']:
                    guid: "${data['guid']}"
                    % elif data['media_type'] in ('show', 'artist'):
                    grandparent_rating_key: "${data['rating_key']}"
                    % elif data['media_type'] in ('season', 'album'):
                    parent_rating_key: "${data['rating_key']}"
                    % elif data['media_type'] in ('movie', 'episode', 'track'):
                    rating_key: "${data['rating_key']}"
                    % elif data['media_type'] in ('collection', 'playlist'):
                    media_type: "${data['media_type']}",
                    rating_key: "${data['rating_key']}"
                    % endif
                };
            }
        }
        history_table = $('#history_table-RK-${data["rating_key"]}').DataTable(history_table_options);
        var colvis = new $.fn.dataTable.ColVis(history_table, { buttonText: '<i class="fa fa-columns"></i> Select columns', buttonClass: 'btn btn-dark', exclude: [0, 12] });
        $(colvis.button()).appendTo('#button-bar-history');

        clearSearchButton('history_table-RK-${data["rating_key"]}', history_table);

        $('#transcode_decision-selection').on('change', function () {
            $('#transcode_decision-selection > label').removeClass('active');
            var selected_filter = $('input[name=transcode_decision-filter]:checked', '#transcode_decision-selection');
            $(selected_filter).closest('label').addClass('active');
            transcode_decision = $(selected_filter).map(function () { return $(this).val(); }).get().join(',');
            setLocalStorage('info_${data["rating_key"]}_history_transcode_decision', transcode_decision);
            history_table.draw();
        });
    }

    var transcode_decision = getLocalStorage('info_${data["rating_key"]}_history_transcode_decision', 'all');
    $.each(transcode_decision.split(','), function (i, item) {
        var history_transcode_decision = $('#history-transcode_decision-' + item.replace(' ', '_'));
        history_transcode_decision.prop('checked', true);
        history_transcode_decision.closest('label').addClass('active');
    });

    $(document).ready(function () {
        loadHistoryTable(transcode_decision);
    });

    $("#refresh-history-list").click(function () {
        history_table.draw();
    });
</script>
% endif
% if data['media_type'] in ('show', 'season', 'artist', 'album', 'photo_album', 'collection', 'playlist'):
<script>
    $.ajax({
        url: 'get_item_children',
        type: 'GET',
        async: true,
        data: {
            rating_key: "${data['rating_key']}",
            media_type: "${data['media_type']}"
        },
        complete: function(xhr, status) {
            $("#children-list").html(xhr.responseText);
        }
    });
</script>
% endif
% if data['media_type'] in ('movie', 'show', 'season', 'episode', 'artist', 'album', 'track', 'collection', 'playlist'):
<script>
    // Populate watch time stats
    $.ajax({
        url: 'item_watch_time_stats',
        async: true,
        data: { 
            % if data['live']:
            guid: "${data['guid']}"
            % else:
            rating_key: "${data['rating_key']}",
            media_type: "${data['media_type']}"
            % endif
        },
        complete: function(xhr, status) {
            $("#watch-time-stats").html(xhr.responseText);
        }
    });
    // Populate user stats
    $.ajax({
        url: 'item_user_stats',
        async: true,
        data: { 
            % if data['live']:
            guid: "${data['guid']}"
            % else:
            rating_key: "${data['rating_key']}",
            media_type: "${data['media_type']}"
            % endif
        },
        complete: function(xhr, status) {
            $("#user-stats").html(xhr.responseText);
        }
    });
</script>
% endif
% if data['media_type'] == 'collection':
<script>
    $.ajax({
        url: 'get_item_children_related',
        type: 'GET',
        async: true,
        data: {
            rating_key: "${data['rating_key']}",
            title: "${data['title']}"
        },
        complete: function(xhr, status) {
            $("#collection-related-list-container").html(xhr.responseText).show();
        }
    });
</script>
% endif
<script>
    $(document).ready(function () {
        // Javascript to enable link to tab
        var hash = document.location.hash;
        var prefix = "tab_";
        if (hash) {
            $('.nav-list #nav-' + hash.replace('#' + prefix, "")).tab('show').trigger('show.bs.tab');
        }

        // Change hash for page-reload
        $('.nav-list a').on('shown.bs.tab', function (e) {
            window.location.hash = e.target.hash.replace("#", "#" + prefix);
        });
    });

    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        $.fn.dataTable.tables({ visible: true, api: true }).columns.adjust();
    });

    var airdate = $("#airdate")
    var runtime = $("#runtime")
    airdate.html(moment(airdate.text()).format('MMM DD, YYYY'));
    runtime.html(humanDuration(runtime.text()));

    $('div.art-face').animate({ opacity: 0.2 }, { duration: 1000 });
    $('#channel-icon').popover({
        selector: '[data-toggle=popover]',
        html: true,
        sanitize: false,
        container: 'body',
        trigger: 'hover',
        placement: 'right',
        template: '<div class="popover channel-thumbnail-popover" role="tooltip"><div class="arrow" style="top: 50%;"></div><div class="popover-content"></div></div>',
        content: function () {
            return '<div class="channel-thumbnail" style="background-image: url(' + $(this).data('img') + ');" />';
        }
    });

    $('.summary-content-summary').hasScrollBar() ? $('.summary-toggle').show() : $('.summary-toggle').hide();

    $('.summary-toggle a').on('click', function() {
        var $this = $(this);
        var $content = $this.parent().prev('div.summary-content-summary');

        if ($this.hasClass('show-more')) {
            $this.html('Read less &nbsp;<i class="fa fa-chevron-up"></i>').toggleClass('show-more').toggleClass('show-less');
            $content.animate({'height': $content[0].scrollHeight});
        } else {
            $this.html('Read more &nbsp;<i class="fa fa-chevron-down"></i>').toggleClass('show-more').toggleClass('show-less');
            $content.animate({'height': '60px'});
        }
    });
</script>
% if _session['user_group'] == 'admin':
<script>
    $("#toggle-export-modal").click(function() {
        $.ajax({
            url: 'export_metadata_modal',
            data: {
                section_id: $(this).data('section_id'),
                rating_key: $(this).data('rating_key'),
                media_type: $(this).data('media_type'),
                sub_media_type: $(this).data('sub_media_type')
            },
            cache: false,
            async: true,
            complete: function(xhr, status) {
                $("#export-modal").html(xhr.responseText);
            }
        });
    });

    function loadExportTable() {
        // Build export table
        export_table_options.ajax = {
            url: 'get_export_list',
            type: 'POST',
            data: function ( d ) {
                return {
                    json_data: JSON.stringify( d ),
                    rating_key: "${data['rating_key']}"
                };
            }
        };
        export_table = $('#export_table-RK-${data["rating_key"]}').DataTable(export_table_options);
        export_table.columns([2, 7]).visible(false);

        var colvis = new $.fn.dataTable.ColVis(export_table, { buttonText: '<i class="fa fa-columns"></i> Select columns', buttonClass: 'btn btn-dark' });
        $(colvis.button()).appendTo('#button-bar-export');

        clearSearchButton('export_table-RK-${data["rating_key"]}', export_table);
    }

    $('#nav-tabs-export').on('shown.bs.tab', function() {
        if (typeof(export_table) === 'undefined') {
            loadExportTable();
        }
    });

    $(document).ready(function () {
        if (!($('#tabs-history').length)) {
            loadExportTable();
        }
    });

    $("#refresh-export-table").click(function () {
        export_table.draw();
    });

    $('#row-edit-mode').on('click', function() {
        if ($(this).hasClass('active')) {
            $(this).tooltip('destroy');

            if (history_to_delete.length > 0) {
                $('#deleteCount').text(history_to_delete.length);
                $('#confirm-modal-delete').modal();
                $('#confirm-modal-delete').one('click', '#confirm-delete', function () {
                    $.ajax({
                        url: 'delete_history_rows',
                        type: 'POST',
                        data: { row_ids: history_to_delete.join(',') },
                        async: true,
                        success: function (data) {
                            var msg = "History deleted";
                            showMsg(msg, false, true, 2000);
                            history_table.draw();
                        }
                    });
                });
            }

            $('.delete-control').each(function () {
                $(this).addClass('hidden');
                $('#row-edit-mode-alert').fadeOut(200);
            });

        } else {
            $(this).tooltip({
                container: '.body-container',
                placement: 'bottom',
                title: 'Select rows to delete. Data is deleted upon exiting delete mode.',
                trigger: 'manual'
            }).tooltip('show');

            history_to_delete = [];
            $('.delete-control').each(function() {
                $(this).find('button.btn-danger').toggleClass('btn-warning').toggleClass('btn-danger');
                $(this).removeClass('hidden');
            });
        }
    });

    // Send recently added notification
    $('#send-recently-added-notification').on('click', function () {
        var rating_key = $(this).data('id');

        $('#send-recently-added-modal').modal();
        $('#send-recently-added-modal').one('click', '#confirm-send-notification', function () {
            $.ajax({
                url: 'send_manual_on_created',
                data: {
                    rating_key: rating_key,
                    notifier_id: $('#send-notification-notifier option:selected').val()
                },
                async: true,
                success: function (data) {
                    if (data.result === 'success') {
                        showMsg('<i class="fa fa-check"></i> ' + data.message, false, true, 5000);
                    } else {
                        showMsg('<i class="fa fa-exclamation-circle"></i> ' + data.message, false, true, 5000, true);
                    }
                }
            });
        });
    });

    $('.metadata-xml').on('tripleclick', function () {
        openPlexXML("/library/metadata/${data['rating_key']}");
    });
</script>
% endif
% if data.get('poster_url'):
<script>
    $('#hosted-poster').popover({
        selector: '[data-toggle=popover]',
        html: true,
        sanitize: false,
        container: 'body',
        trigger: 'hover',
        placement: 'top',
        template: '<div class="popover history-thumbnail-popover" role="tooltip"><div class="arrow" style="top: 50%;"></div><div class="popover-content"></div></div>',
        content: function () {
            return '<div class="history-thumbnail" style="background-image: url(' + $(this).data('img') + '); height: ' + $(this).data('height') + 'px; width: ' + $(this).data('width') + 'px;" />';
        }
    });

    $('#delete-hosted-poster').on('click', function () {
        var msg = 'Are you sure you want to delete the ${data['img_service']} poster for <strong>' + $(this).data('title') + '</strong>?<br><br>' +
                  'All previous links to this image will no longer work.';
        var url = 'delete_hosted_images';
        var data = { rating_key: $(this).data('id') };
        var callback = function () {
            $('.hosted-poster-tooltip').popover('destroy');
            $('#delete-hosted-poster').closest('.btn-group').remove();
        };
        confirmAjaxCall(url, msg, data, false, callback);
    });
</script>
% endif
% if data.get('tvmaze_id') or data.get('themoviedb_id') or data.get('musicbrainz_id'):
<script>
    $('#delete-lookup-info').on('click', function () {
        var msg = 'Are you sure you want to delete all the metadata lookup info for <strong>' + $(this).data('title') + '</strong>?' +
                  '<br /><br />Tautulli will lookup the metadata info again the next time a notification is sent.';
        var url = 'delete_lookup_info';
        var data = { rating_key: $(this).data('id') };
        var callback = function () {
            $('#delete-lookup-info').closest('.btn-group').remove();
        };
        confirmAjaxCall(url, msg, data, false, callback);
    });
</script>
% endif
% endif
</%def>
